import { IncomingMessage, ServerResponse } from 'http';
import { loginUser } from 'mock/configs';
import { accountList } from '../Account/account';

/** 登录 */
const login = [
  {
    url: '/api/login',
    method: 'post',
    response: (options: { body: Record<string, any> }) => {
      const { body } = options;
      let res = null;
      if (body.account && body.password) {
        let isDisabled = false;
        accountList.forEach(p => {
          if(p.account == body.account) {
            res = p;
            if(p.disabled) isDisabled = true;
          }
        })
        if(!res) return { code: 402, errMsg: '账号未注册，请联系管理员注册账号' };
        else if(isDisabled) return { code: 401, errMsg: '您的账号已被禁用' }
        else {
          for(const key in loginUser) {
            loginUser[key] = res[key];
          }
          return { code: 200, data: res };
        }
      }
    }
  },

  /** 退出登录 */
  {
    url: '/api/loginOut',
    method: 'post',
    rawResponse: (req: IncomingMessage, res: ServerResponse) => {
      res.setHeader('Content-Type', 'text/plain');
      res.end(`退出成功`);
    }
  }
];

export default login;
